import {RollNotice, RollNoticeItem} from 'vue-ydui/dist/lib.rem/rollnotice'
import {BackTop} from 'vue-ydui/dist/lib.rem/backtop'
import {Input} from 'vue-ydui/dist/lib.rem/input'
import {CellGroup, CellItem} from 'vue-ydui/dist/lib.rem/cell'
import { Toast, Indicator, MessageBox } from 'mint-ui'
import {Step, StepItem} from 'vue-ydui/dist/lib.rem/step'
import {Radio, RadioGroup} from 'vue-ydui/dist/lib.rem/radio'
import {CitySelect} from 'vue-ydui/dist/lib.rem/cityselect'
import {TextArea} from 'vue-ydui/dist/lib.rem/textarea'
import {CheckBox, CheckBoxGroup} from 'vue-ydui/dist/lib.rem/checkbox'
import {Popup} from 'vue-ydui/dist/lib.rem/popup'
import {LightBox, LightBoxImg, LightBoxTxt} from 'vue-ydui/dist/lib.rem/lightbox'
import {ScrollNav, ScrollNavPanel} from 'vue-ydui/dist/lib.rem/scrollnav'

import {moment, http, mergeData} from './util'
import qs from 'querystring'

export default {
  install: (Vue) => {
    Vue.prototype.$message = {
      toast: Toast,
      loading(loading) {
        loading ? Indicator.open() : Indicator.close()
      },
      confirm: MessageBox.confirm,
      alert: (message, title) => MessageBox.alert(message, title || '提示')
    }
    Vue.prototype.$regex = {
      phone: /^1[3456789]\d{9}$/, // 手机号
      idCard: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/, // 身份证号
      password: /^[a-zA-Z_\d]{6,16}$/
    }
    Vue.prototype.$file = GLOBAL_CONF.STATIC_URL
    Vue.prototype.$dateFormat = moment
    Vue.prototype.$post = (url, params = {}, fn, tip = true) => http({method: 'post', data: params, url, tip}, fn)
    Vue.prototype.$getImg = (path, params = {}) => {
      mergeData(params)
      return `${GLOBAL_CONF.BASE_URL}/sapi/getImg/show/${path}?${qs.stringify(params)}`
    }
    Vue.directive('focus', {
      // 当被绑定的元素插入到 DOM 中时……
      inserted: function (el, binding) {
        // 聚焦元素
        if (el.tagName.toLowerCase() === 'input' || el.tagName.toLowerCase() === 'textarea') {
          el.focus()
        } else if (el.getElementsByTagName('input')[0]) {
          el.getElementsByTagName('input')[0].focus()
        } else if (el.getElementsByTagName('textarea')[0]) {
          el.getElementsByTagName('textarea')[0].focus()
        }
      }
    })
    Vue.component(RollNotice.name, RollNotice)
    Vue.component(RollNoticeItem.name, RollNoticeItem)
    Vue.component(BackTop.name, BackTop)
    Vue.component(Input.name, Input)
    Vue.component(CellGroup.name, CellGroup)
    Vue.component(CellItem.name, CellItem)
    Vue.component(Step.name, Step)
    Vue.component(StepItem.name, StepItem)
    Vue.component(Radio.name, Radio)
    Vue.component(RadioGroup.name, RadioGroup)
    Vue.component(CitySelect.name, CitySelect)
    Vue.component(TextArea.name, TextArea)
    Vue.component(CheckBox.name, CheckBox)
    Vue.component(CheckBoxGroup.name, CheckBoxGroup)
    Vue.component(Popup.name, Popup)
    Vue.component(LightBox.name, LightBox)
    Vue.component(LightBoxImg.name, LightBoxImg)
    Vue.component(LightBoxTxt.name, LightBoxTxt)
    Vue.component(ScrollNav.name, ScrollNav)
    Vue.component(ScrollNavPanel.name, ScrollNavPanel)
  }
}
